package com.fjl.service;

import com.alibaba.fastjson2.JSON;
import com.fjl.LogUtil;
import com.fjl.exception.AbpmServiceException;
import com.fjl.init.AbpmGlobalInfo;
import com.fjl.mapper.AbpmMapper;
import com.fjl.pojo.AsyncTaskDetail;
import com.fjl.pojo.NextTaskDetail;
import com.fjl.pojo.ServiceResultInAbxMultiBranch;
import com.fjl.pojo.StepInfoInAbxMultiBranch;
import com.fjl.pojo.po.AbpmRunTaskPO;
import com.fjl.result.AbpmServiceResults;
import com.fjl.utils.IdGenerator;
import com.fjl.utils.ParseBPMN;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.fjl.service.NodeProperteyNameConstantsInAbxMultiBranch.*;


/**
 * ClassName: AbpmServiceInABXMultiBranch
 * Package: cn.com.agree.abpm
 * Description:
 *
 * @Author 申航
 * @Create 2024/11/27 11:02
 * @Version 1.0
 */

@Service
public class AbpmServiceInABXMultiBranch {

    @Autowired
    private AbpmMapper abpmMapper;

    @Autowired
    private ParseBPMN parseBPMN;


    @Autowired
    IdGenerator idGenerator ;

//    @Transactional
    public ServiceResultInAbxMultiBranch startProcess(String definitionKey, String businessKey, String variablesJson) {
        LogUtil.info("startProcess_start");
        ServiceResultInAbxMultiBranch result = new ServiceResultInAbxMultiBranch();
        result.exception(AbpmServiceResults.SUC000_调用成功);
        result.setBusinessKey(businessKey);

        Process process = AbpmGlobalInfo.processMap.get(definitionKey);
        if (process == null) {
            result.exception(AbpmServiceResults.BIZ006_流程定义不存在);
            return result;
        }

        // 该任务对应父任务id
        String parentTaskId = null;
        String processInstanceId = String.valueOf(idGenerator.nextID());
        boolean isEnd = false;
        ArrayList<AbpmRunTaskPO> abpmRunTaskPOList = new ArrayList<>();

        // 获取下一个任务
        NextTaskDetail nextTaskDetail = new NextTaskDetail();
        List<StartEvent> flowElementsOfType = process.findFlowElementsOfType(StartEvent.class);
        if (flowElementsOfType != null && flowElementsOfType.size() > 1) {
            result.exception(AbpmServiceResults.PRC005_流程定义异常);
            return result;
        }
        parseBPMN.nextTask(flowElementsOfType.get(0), process, variablesJson, processInstanceId, null, nextTaskDetail);

        // 创建任务
        for (NextTaskDetail.TaskDetail taskDetail : nextTaskDetail.getTaskDetailList()) {
            FlowNode task = taskDetail.getTask();
            String currentProcessId = taskDetail.getProcDefId();
            AbpmRunTaskPO abpmRunTaskPO = parseBPMN.createTask(null, task.getId(), currentProcessId, null, processInstanceId, parentTaskId, null, definitionKey);
            abpmRunTaskPOList.add(abpmRunTaskPO);
            // 子任务创建处理
            if (taskDetail.getSubProcessTaskDetail() != null) {
                parentTaskId = abpmRunTaskPO.getId();
                creatSubProcessTask( taskDetail.getSubProcessTaskDetail().getTaskDetailList(),abpmRunTaskPOList,parentTaskId,processInstanceId,definitionKey);
//                for (NextTaskDetail.TaskDetail subProcessTaskDetail : taskDetail.getSubProcessTaskDetail().getTaskDetailList()) {
//                    FlowNode subTask = subProcessTaskDetail.getTask();
//                    String subCurrentProcessId = subProcessTaskDetail.getProcDefId();
//                    AbpmRunTaskPO subAbpmRunTaskPO = parseBPMN.createTask(null, subTask.getId(), subCurrentProcessId, null, processInstanceId, parentTaskId, null, definitionKey);
//                    abpmRunTaskPOList.add(subAbpmRunTaskPO);
//                }
            }
            parentTaskId = "";
        }

        result.setRootProcessInstanceId(processInstanceId);
        result.setEnded(isEnd);
        result.setDefinitionKey(process.getId());
        result.setDefinitionCaption(process.getName());
        result.setProcessVersion(process.getDocumentation());
        refreshResult(result, nextTaskDetail.getTaskDetailList(), abpmRunTaskPOList, variablesJson);
        LogUtil.info("startProcess_end");
        return result;
    }

    private void creatSubProcessTask(List<NextTaskDetail.TaskDetail> taskDetailList, ArrayList<AbpmRunTaskPO> abpmRunTaskPOList, String parentTaskId, String processInstanceId,String definitionKey) {
        for (NextTaskDetail.TaskDetail subProcessTaskDetail : taskDetailList) {
            FlowNode subTask = subProcessTaskDetail.getTask();
            String subCurrentProcessId = subProcessTaskDetail.getProcDefId();
            AbpmRunTaskPO subAbpmRunTaskPO = parseBPMN.createTask(null, subTask.getId(), subCurrentProcessId, null, processInstanceId, parentTaskId, null, definitionKey);
            abpmRunTaskPOList.add(subAbpmRunTaskPO);
            if (subProcessTaskDetail.getSubProcessTaskDetail() != null){
                creatSubProcessTask( subProcessTaskDetail.getSubProcessTaskDetail().getTaskDetailList(),abpmRunTaskPOList,subAbpmRunTaskPO.getId(),processInstanceId,definitionKey);
            }
        }
    }

    private void refreshResult(ServiceResultInAbxMultiBranch result, List<NextTaskDetail.TaskDetail> TaskDetailList, ArrayList<AbpmRunTaskPO> abpmRunTaskPOList, String variablesJson){
        LogUtil.info("refreshResult_start");
        List<AsyncTaskDetail> asyncTaskDetailList = new ArrayList<>();
        for (NextTaskDetail.TaskDetail taskDetail : TaskDetailList) {
            if (taskDetail.isCallActivityFlag()) {
                List<NextTaskDetail.TaskDetail> subProcessTaskDetailList = taskDetail.getSubProcessTaskDetail().getTaskDetailList();
                for (NextTaskDetail.TaskDetail subProcessTaskDetail : subProcessTaskDetailList) {
                    if(subProcessTaskDetail.isCallActivityFlag()){
                        refreshSubProcessResult(asyncTaskDetailList,subProcessTaskDetail.getSubProcessTaskDetail().getTaskDetailList(),abpmRunTaskPOList,variablesJson);
                    }else{
                        UserTask subTask = (UserTask) subProcessTaskDetail.getTask();
                        asyncTaskDetailList.add(asyncTaskDetail(subTask, subProcessTaskDetail, abpmRunTaskPOList, variablesJson));
                    }
                }
            }else {
                UserTask task = (UserTask) taskDetail.getTask();
                AsyncTaskDetail asyncTaskDetail = asyncTaskDetail(task, taskDetail, abpmRunTaskPOList, variablesJson);
                asyncTaskDetailList.add(asyncTaskDetail);
            }

        }
        result.setTaskDetailList(asyncTaskDetailList);
        LogUtil.info("refreshResult_end");
    }

    private void refreshSubProcessResult(List<AsyncTaskDetail> asyncTaskDetailList, List<NextTaskDetail.TaskDetail> subProcessTaskDetailList, ArrayList<AbpmRunTaskPO> abpmRunTaskPOList, String variablesJson) {
        for (NextTaskDetail.TaskDetail subProcessTaskDetail : subProcessTaskDetailList) {
            if(subProcessTaskDetail.isCallActivityFlag()){
                refreshSubProcessResult(asyncTaskDetailList,subProcessTaskDetail.getSubProcessTaskDetail().getTaskDetailList(), abpmRunTaskPOList,variablesJson);
            }else{
                UserTask subTask = (UserTask) subProcessTaskDetail.getTask();
                asyncTaskDetailList.add(asyncTaskDetail(subTask, subProcessTaskDetail, abpmRunTaskPOList, variablesJson));
            }
        }

    }

//    @Transactional
    public ServiceResultInAbxMultiBranch continueProcess(String taskId, String businessKey, String variablesJson) {
        LogUtil.info("交易提交,taskId={},businessKey={},variablesJson={}",taskId,businessKey,variablesJson);
        ServiceResultInAbxMultiBranch result = new ServiceResultInAbxMultiBranch();
        result.exception(AbpmServiceResults.SUC000_调用成功);
        result.setBusinessKey(businessKey);
        // 流程对应id
        String procDefId = "";
        // 任务节点id
        String name = "";
        // 执行器id
        String executionId = "";
        // 流程全局唯一id
        String processInstanceId = "";
        // 该任务对应父任务id
        String parentTaskId = "";
        // 模块名称
        String moduleName = "";
        // 流程是否结束
        boolean isEnd = false;
        // 当前任务所属总流程id
        String rootProcessId = "";
        ArrayList<AbpmRunTaskPO> AbpmRunTaskPOList = new ArrayList<>();

        // 查询该任务id所在的全部信息
        AbpmRunTaskPO abpmRunTaskPO = abpmMapper.selectRuTaskById(taskId);
        if (abpmRunTaskPO != null) {
            procDefId = abpmRunTaskPO.getProcDefId();
            name = abpmRunTaskPO.getName();
            executionId = abpmRunTaskPO.getExecutionId();
            processInstanceId = abpmRunTaskPO.getProcessInstanceId();
            parentTaskId = abpmRunTaskPO.getParentTaskId();
            moduleName = abpmRunTaskPO.getModuleName();
            rootProcessId = abpmRunTaskPO.getRootProcessId();
            // 删除已完成的任务
            Integer i = abpmMapper.deleteRuTaskById(taskId);
            if (i != 0) {
                LogUtil.info("删除成功");
            }
        }
        // 获取当前任务所在的流程图
        Process currentTaskProcess = AbpmGlobalInfo.processMap.get(procDefId);
        // 定位当前任务所在流程图的节点
        FlowNode currentTaskFlowNode = (FlowNode) currentTaskProcess.getFlowElement(name);
        // 下一步任务相关信息
        NextTaskDetail nextTaskDetail = new NextTaskDetail();
        parseBPMN.nextTask(currentTaskFlowNode, currentTaskProcess, variablesJson, processInstanceId, parentTaskId, nextTaskDetail);
        // 创建任务
        for (NextTaskDetail.TaskDetail taskDetail : nextTaskDetail.getTaskDetailList()) {
            FlowNode task = taskDetail.getTask();
            String currentProcessId = taskDetail.getProcDefId();
            AbpmRunTaskPO AbpmRunTaskPO;
            if (currentProcessId.equals(procDefId)) {
                AbpmRunTaskPO = parseBPMN.createTask(null, task.getId(), currentProcessId, null, processInstanceId, parentTaskId, null, rootProcessId);
            }else {
                AbpmRunTaskPO = parseBPMN.createTask(null, task.getId(), currentProcessId, null, processInstanceId, nextTaskDetail.getParentId(), null, rootProcessId);
            }
            AbpmRunTaskPOList.add(AbpmRunTaskPO);
            // TODO 子任务创建处理
//            creatSubProcessTask( taskDetail.getSubProcessTaskDetail().getTaskDetailList(),abpmRunTaskPOList,parentTaskId,processInstanceId,definitionKey);
            creatSubTask(taskDetail, AbpmRunTaskPO, processInstanceId, rootProcessId, AbpmRunTaskPOList);
            parentTaskId = "";
        }

        Process rootProcess = AbpmGlobalInfo.processMap.get(rootProcessId);

        result.setRootProcessInstanceId(processInstanceId);
        result.setEnded(nextTaskDetail.isEnd());
        result.setExceptionallyEnded(nextTaskDetail.isErrorStop());
        result.setDefinitionKey(rootProcess.getId());
        result.setProcessVersion(rootProcess.getDocumentation());
        result.setDefinitionCaption(rootProcess.getName());
        refreshResult(result, nextTaskDetail.getTaskDetailList(), AbpmRunTaskPOList, variablesJson);
        return result;
    }

    private void creatSubTask(NextTaskDetail.TaskDetail taskDetail, AbpmRunTaskPO AbpmRunTaskPO, String processInstanceId, String rootProcessId, ArrayList<AbpmRunTaskPO> AbpmRunTaskPOList) {
        String parentTaskId;
        if (taskDetail.getSubProcessTaskDetail() != null) {
            parentTaskId = AbpmRunTaskPO.getId();
            for (NextTaskDetail.TaskDetail subProcessTaskDetail : taskDetail.getSubProcessTaskDetail().getTaskDetailList()) {
                FlowNode subTask = subProcessTaskDetail.getTask();
                String subCurrentProcessId = subProcessTaskDetail.getProcDefId();
                AbpmRunTaskPO subAbpmRunTaskPO = parseBPMN.createTask(null, subTask.getId(), subCurrentProcessId, null, processInstanceId, parentTaskId, null, rootProcessId);
                AbpmRunTaskPOList.add(subAbpmRunTaskPO);
                if (subProcessTaskDetail.getSubProcessTaskDetail() != null) {
                    creatSubTask(subProcessTaskDetail, subAbpmRunTaskPO, processInstanceId, rootProcessId, AbpmRunTaskPOList);
                }
            }
            parentTaskId="";
        }
    }

    private AsyncTaskDetail asyncTaskDetail(UserTask task, NextTaskDetail.TaskDetail taskDetail, ArrayList<AbpmRunTaskPO> AbpmRunTaskPOList, String variablesJson) {
        AsyncTaskDetail asyncTaskDetail = new AsyncTaskDetail();
        asyncTaskDetail.setActivityIdOfFirstLevel(task.getId());
        asyncTaskDetail.setActivityNameOfFirstLevel(task.getName());
        asyncTaskDetail.setCurrentDefinitionKey(taskDetail.getProcDefId());
        String stepDefinitionKey = taskDetail.getProcDefId() + "_" + task.getId();
        Map<String, String> procDefId = AbpmGlobalInfo.formpropertiesMap.get(stepDefinitionKey);
        if (procDefId.containsKey(支线任务标志)){
            asyncTaskDetail.setSideTaskFlag(procDefId.get(支线任务标志));
        }
        if (procDefId.containsKey(支线任务生成服务码)){
            asyncTaskDetail.setSideTaskServiceCode(procDefId.get(支线任务生成服务码));
        }
        if (procDefId.containsKey(任务摘要模块码)){
            asyncTaskDetail.setTaskAbstractCode(procDefId.get(任务摘要模块码));
        }
        if (procDefId.containsKey(任务摘要字段配置)){
            asyncTaskDetail.setTaskAbstractInfoList(new ArrayList<Object>(Arrays.asList(JSON.parseArray(procDefId.get(任务摘要字段配置)).toArray())));
        }
        if (procDefId.containsKey(任务分配规则)){
            asyncTaskDetail.setTaskAllocationRule(procDefId.get(任务分配规则));
        }
        if (procDefId.containsKey(退回标志)){
            asyncTaskDetail.setTaskBackFlag(procDefId.get(退回标志));
        }
        if (procDefId.containsKey(任务处理岗位)){
            asyncTaskDetail.setTaskDealPost(procDefId.get(任务处理岗位));
        }
        if (procDefId.containsKey(任务通知服务码)){
            asyncTaskDetail.setTaskNoticeCode(procDefId.get(任务通知服务码));
        }
        if (procDefId.containsKey(任务登记服务码)){
            asyncTaskDetail.setTaskRegisterCode(procDefId.get(任务登记服务码));
        }
        if (procDefId.containsKey(任务登记字段配置)){
            asyncTaskDetail.setTaskRegisterInfoList(new ArrayList<Object>(Arrays.asList(JSON.parseArray(procDefId.get(任务登记字段配置)).toArray())));
        }
        if (procDefId.containsKey(任务场景码)){
            asyncTaskDetail.setTaskSceneCode(procDefId.get(任务场景码));
        }
        if (procDefId.containsKey(任务生成服务码)){
            asyncTaskDetail.setTaskServiceCode(procDefId.get(任务生成服务码));
        }
        if (procDefId.containsKey(任务提交字段配置)){
            asyncTaskDetail.setTaskSubmitInfoList(new ArrayList<Object>(Arrays.asList(JSON.parseArray(procDefId.get(任务提交字段配置)).toArray())));
        }
        if (procDefId.containsKey(任务生成类型)){
            asyncTaskDetail.setTaskType(procDefId.get(任务生成类型));
        }
        if (procDefId.containsKey(任务重新分配规则)){
            asyncTaskDetail.setTaskReAllocationRule(procDefId.get(任务重新分配规则));
        }
        if (procDefId.containsKey(规则名称)){
            asyncTaskDetail.setRuleName(procDefId.get(规则名称));
        }
        if (procDefId.containsKey(节点处理登记)){
            asyncTaskDetail.setNodeProcessingRegistration(procDefId.get(节点处理登记));
        }
        if (procDefId.containsKey(version)){
            asyncTaskDetail.setUserTaskVersion(procDefId.get(version));
        }
        if (procDefId.containsKey(规则类型)){
            asyncTaskDetail.setRuleType(procDefId.get(规则类型));
        }
        if (procDefId.containsKey(节点提交字段配置)){
            asyncTaskDetail.setNodeSubmissionFieldConfiguration(procDefId.get(节点提交字段配置));
        }

        asyncTaskDetail.setProcessVersion(taskDetail.getProcessVersion());

        List<AbpmRunTaskPO> taskKey = AbpmRunTaskPOList.stream()
                .filter(Predicate -> Predicate.getName().equals(task.getId()))
                .filter(Predicate -> Predicate.getProcDefId().equals(taskDetail.getProcDefId()))
                .collect(Collectors.toList());

        AbpmRunTaskPO asyncTaskDetail1 = taskKey.get(0);
        asyncTaskDetail.setTaskExecutionId(asyncTaskDetail1.getExecutionId());
        asyncTaskDetail.setTaskId(asyncTaskDetail1.getId());
        asyncTaskDetail.setTaskKey(task.getId());
        asyncTaskDetail.setTaskName(task.getName());
        asyncTaskDetail.setTaskProcessInstanceId(asyncTaskDetail1.getProcessInstanceId());

        asyncTaskDetail.setStepBpmnProcessDefinitionKey(stepDefinitionKey);

        Process process1 = AbpmGlobalInfo.processMap.get(stepDefinitionKey);
        if (process1 != null ) {
            List<StepInfoInAbxMultiBranch> stepInfoInAbxMultiBranches = parseBPMN.futureStepInfoList(process1, variablesJson);
            asyncTaskDetail.setFutureStepInfoList(stepInfoInAbxMultiBranches);
        }
        return asyncTaskDetail;
    }

    public ServiceResultInAbxMultiBranch stopProcess(String taskId) {
        LogUtil.info("关闭交易，taskId"+taskId);
        ServiceResultInAbxMultiBranch result = new ServiceResultInAbxMultiBranch();

        try{
            // 查询该任务id所在的全部信息
            AbpmRunTaskPO abpmRunTaskPO = abpmMapper.selectRuTaskById(taskId);
            if (abpmRunTaskPO != null) {
                String processInstanceId = abpmRunTaskPO.getProcessInstanceId();
                // 删除已完成的任务
                Integer i = abpmMapper.deleteRUTaskByProcessInstanceId(processInstanceId);
                if (i != 0) {
                    LogUtil.info("删除成功");
                }
                result.setResultCode("SUC000");
                result.setResultMessage("调用成功");
            }else{
                result.exception(AbpmServiceResults.TSK000_任务已被消费);
            }

        }catch (Exception e){
            result.exception(new AbpmServiceException(e.getMessage(),e));
        }

        return result;
    }

    public ServiceResultInAbxMultiBranch queryCurrentTaskInfo(String rootProcessInstanceId) {
        LogUtil.info("queryCurrentTaskInfo_start");
        ServiceResultInAbxMultiBranch result = new ServiceResultInAbxMultiBranch();
        result.exception(AbpmServiceResults.SUC000_调用成功);

        List<AbpmRunTaskPO> abpmRunTaskPOList = abpmMapper.selectRuTaskByProcessInstanceId(rootProcessInstanceId);
        if (abpmRunTaskPOList == null || abpmRunTaskPOList.size() <= 0) {
            LogUtil.info("该任务不存在");
            result.exception(AbpmServiceResults.TSK001_任务不存在);
            return result;
        }
        NextTaskDetail nextTaskDetail = new NextTaskDetail();
        ArrayList<NextTaskDetail.TaskDetail> taskDetailsList = new ArrayList<>();
        for (AbpmRunTaskPO abpmRunTaskPO : abpmRunTaskPOList) {
            NextTaskDetail.TaskDetail taskDetail = nextTaskDetail.new TaskDetail();
            Process process = AbpmGlobalInfo.processMap.get(abpmRunTaskPO.getProcDefId());
            FlowElement flowElement = process.getFlowElement(abpmRunTaskPO.getName());
            if (flowElement instanceof CallActivity) {
                continue;
            }
            taskDetail.setTask((FlowNode) flowElement);
            taskDetail.setProcessVersion(process.getDocumentation());
            taskDetail.setProcDefId(process.getId());
            taskDetailsList.add(taskDetail);
        }
        nextTaskDetail.setTaskDetail(taskDetailsList);


        Process rootProcess = AbpmGlobalInfo.processMap.get(abpmRunTaskPOList.get(0).getRootProcessId());

        result.setRootProcessInstanceId(abpmRunTaskPOList.get(0).getProcessInstanceId());
        result.setEnded(nextTaskDetail.isEnd());
        result.setExceptionallyEnded(nextTaskDetail.isErrorStop());
        result.setDefinitionKey(rootProcess.getId());
        result.setProcessVersion(rootProcess.getDocumentation());
        result.setDefinitionCaption(rootProcess.getName());
        refreshResult(result, nextTaskDetail.getTaskDetailList(), (ArrayList<AbpmRunTaskPO>) abpmRunTaskPOList, "");
        LogUtil.info("queryCurrentTaskInfo_end");
        return result;
    }

    public ServiceResultInAbxMultiBranch restartProcessWithAsync(String definitionKey, String rootProcessInstanceId, String variablesJson) {
        LogUtil.info("restartProcessWithAsync_start");
        ServiceResultInAbxMultiBranch result = new ServiceResultInAbxMultiBranch();
        result.exception(AbpmServiceResults.SUC000_调用成功);

        List<AbpmRunTaskPO> abpmRunTaskPOList = abpmMapper.selectRuTaskByProcessInstanceIdAndProcDefId(rootProcessInstanceId, definitionKey);
        if (abpmRunTaskPOList == null || abpmRunTaskPOList.size() <= 0) {
            LogUtil.info("需重置流程不存在");
            result.exception(AbpmServiceResults.STEP004_当前步骤无法重置);
            return result;
        }

        // 重置的为一级流程
        if (abpmRunTaskPOList.get(0).getParentTaskId() == null) {
            stopProcess(abpmRunTaskPOList.get(0).getId());
            return startProcess(abpmRunTaskPOList.get(0).getRootProcessId(), "", variablesJson);
        }

        String parentTaskId = abpmRunTaskPOList.get(0).getParentTaskId();
        String processInstanceId = abpmRunTaskPOList.get(0).getProcessInstanceId();

        // 重置的为二级流程
        Process process = AbpmGlobalInfo.processMap.get(abpmRunTaskPOList.get(0).getProcDefId());
        List<StartEvent> flowElementsOfType = process.findFlowElementsOfType(StartEvent.class);
        if (flowElementsOfType != null && flowElementsOfType.size() > 1) {
            result.exception(AbpmServiceResults.PRC005_流程定义异常);
            return result;
        }
        NextTaskDetail nextTaskDetail = new NextTaskDetail();
        parseBPMN.nextTask(flowElementsOfType.get(0), process, variablesJson, processInstanceId, parentTaskId, nextTaskDetail);

        abpmMapper.deleteRuTaskByProcessInstanceIdAndProcDefId(processInstanceId, abpmRunTaskPOList.get(0).getProcDefId());

        // 创建任务
        for (NextTaskDetail.TaskDetail taskDetail : nextTaskDetail.getTaskDetailList()) {
            FlowNode task = taskDetail.getTask();
            String currentProcessId = taskDetail.getProcDefId();
            AbpmRunTaskPO abpmRunTaskPO = parseBPMN.createTask(null, task.getId(), currentProcessId, null, processInstanceId, parentTaskId, null, abpmRunTaskPOList.get(0).getRootProcessId());
            abpmRunTaskPOList.add(abpmRunTaskPO);
            // 子任务创建处理
            if (taskDetail.getSubProcessTaskDetail() != null) {
                parentTaskId = abpmRunTaskPO.getId();
                creatSubProcessTask( taskDetail.getSubProcessTaskDetail().getTaskDetailList(), (ArrayList<AbpmRunTaskPO>) abpmRunTaskPOList,parentTaskId,processInstanceId, abpmRunTaskPOList.get(0).getRootProcessId());
            }
            parentTaskId = "";
        }

        Process rootProcess = AbpmGlobalInfo.processMap.get(abpmRunTaskPOList.get(0).getRootProcessId());

        result.setRootProcessInstanceId(processInstanceId);
        result.setEnded(false);
        result.setDefinitionKey(rootProcess.getId());
        result.setDefinitionCaption(rootProcess.getName());
        result.setProcessVersion(rootProcess.getDocumentation());
        refreshResult(result, nextTaskDetail.getTaskDetailList(), (ArrayList<AbpmRunTaskPO>) abpmRunTaskPOList, variablesJson);
        LogUtil.info("restartProcessWithAsync_end");
        return result;
    }


//    public ServiceResultInAbxMultiBranch jumpProcessAsync(String taskId, String businessKey, String targetTaskKey, String targetProcDefId, String variablesJson) {
//        LogUtil.info("流程跳转");
//        ServiceResultInAbxMultiBranch result = new ServiceResultInAbxMultiBranch();
//        result.exception(AbpmServiceResults.SUC000_调用成功);
//        result.setBusinessKey(businessKey);
//        ArrayList<Map<String, Object>> taskAndProcDefIdList = new ArrayList<>();
//        ArrayList<Map<String, Object>> taskDetailList = new ArrayList<>();
//
//        Process process = AbpmGlobalInfo.processMap.get(targetProcDefId);
//        if (process == null) {
//            result.setResultDetail("流程定义不存在");
//            return result;
//        }
//        Collection<FlowElement> flowElements = process.getFlowElements();
//        boolean jumpFlag = false;
//        for (FlowElement flowElement : flowElements) {
//            if (flowElement instanceof FlowNode && flowElement.getId().equals(targetTaskKey)) {
//                Map<String, Object> taskAndProcDefId = new HashMap<>();
//                taskAndProcDefId.put("procDefId", process.getId());
//                taskAndProcDefId.put("task", (FlowNode) flowElement);
//                taskAndProcDefId.put("isCallActivity", false);
//                taskAndProcDefIdList.add(taskAndProcDefId);
//                jumpFlag = true;
//            }
//        }
//        if (!jumpFlag) {
//            LogUtil.info("流程跳转失败，请检查传参");
//        }
//        // 查询该任务id所在的全部信息
//        AbpmRunTaskPO abpmRunTaskPO = abpmMapper.selectRuTaskById(taskId);
//        if (abpmRunTaskPO == null) {
//            LogUtil.info("任务不存在，请检查任务 id 是否正确");
//        }
//        // 当前当前任务为嵌套流程并且需要跳转的任务节点在子流程图中
//        if (abpmRunTaskPO.getParentTaskId() != null && abpmRunTaskPO.getProcDefId().equals(targetProcDefId)) {
//            abpmMapper.deleteRUTaskByParentTaskId(abpmRunTaskPO.getParentTaskId());
//            Map<String, Object> taskDetail = parseBPMN.createTask(null, targetTaskKey, targetProcDefId, null, abpmRunTaskPO.getProcessInstanceId(), abpmRunTaskPO.getParentTaskId(), null, abpmRunTaskPO.getRootProcessId());
//            taskDetailList.add(taskDetail);
//        }
//        AbpmRunTaskPO subAbpmRunTaskPO = abpmMapper.selectRuTaskById(abpmRunTaskPO.getParentTaskId());
//        // 当前任务不为嵌套任务或需要需要跳转到的任务节点为主流程图中
//        if (abpmRunTaskPO.getParentTaskId() == null || targetProcDefId.equals(subAbpmRunTaskPO.getProcDefId())){
//            abpmMapper.deleteRUTaskByProcessInstanceId(abpmRunTaskPO.getProcessInstanceId());
//            Map<String, Object> taskDetail = parseBPMN.createTask(null, targetTaskKey, targetProcDefId, null, abpmRunTaskPO.getProcessInstanceId(), null, null, abpmRunTaskPO.getRootProcessId());
//            taskDetailList.add(taskDetail);
//        }
//
//        result.setRootProcessInstanceId(abpmRunTaskPO.getProcessInstanceId());
//        result.setEnded(false);
//        result.setDefinitionKey(targetTaskKey);
//        result.setDefinitionCaption(process.getName());
//        refreshResult(result, taskAndProcDefIdList, taskDetailList, variablesJson);
//        return result;
//    }
}
